package com.jorenwu.asymmetric;

import java.io.OutputStream;
import java.io.Writer;
import java.math.BigInteger;

public class RSAEncoder
{
//	int ex_Nbits;
//	public RSAEncoder(int nbits)
//	{
//		this.ex_Nbits=nbits/8+1;
//	}
	
	public BigInteger stringToInteger(String str)
	{
		return new BigInteger(str);
	}

	public String inttostring(int ch)
	{
		return Integer.toString(ch);
	}

	public BigInteger inputBlockEncrypto(int ch)
	{
		return stringToInteger(inttostring(ch));
	}

	public void outputBlockEncrypto(OutputStream out, BigInteger bi)
	{
		byte[] src = bi.toByteArray();
		byte[] dest = new byte[129];
		if (src.length == 129)
		{
			System.arraycopy(src, 0, dest, 0, 129);
		} else
		{
			for(int i=0;i<129;i++)
			{
				dest[i]=0;
			}
			System.arraycopy(src, 0, dest, 129-src.length, src.length);
		}
		try
		{
			out.write(dest);
			out.flush();
		} catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}

	public BigInteger inputBlockDecrypto(byte[] bt)
	{
		return new BigInteger(bt);
	}

	public void outputBlockDecrypto(Writer writer, BigInteger bi)
	{
		try
		{
			writer.write(bi.intValue());
		} catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
